/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.app.dao;

import com.sun.org.apache.bcel.internal.generic.RETURN;
import edu.app.persistence.Administrateur;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import edu.app.gui.Auth;
import javax.swing.JTable;
import net.proteanit.sql.DbUtils;
import sun.font.TextLabel;

/**
 *
 * @author esprit
 */
public class AdministrateurDAO extends IDAO<Administrateur> {

    @Override
    public Administrateur find(String login) {
        String sql = "SELECT * FROM ADMINISTRATEUR WHERE LOGIN=?";
        Administrateur found = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, login);
            resultSet = preparedStatement.executeQuery(); 
            System.out.println(sql);
            if (resultSet.next()) {
                found = new Administrateur(resultSet.getString("login"), resultSet.getString("motdepasse"), resultSet.getString("email"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(UtilisateurDAO.class.getName()).log(Level.SEVERE, "find ADMIN failed", ex);
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(UtilisateurDAO.class.getName()).log(Level.SEVERE, "free resourses failed", ex);
            }
        }
        return found;
    }
    

    @Override
    public void create(Administrateur d) {
        if (find(d.getLogin()) == null) {
            
            String sql = "INSERT INTO ADMINISTRATEUR (LOGIN, MOTDEPASSE, EMAIL) VALUES (?,?,?)";
            PreparedStatement preparedStatement = null;
            try {
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, d.getLogin());
                preparedStatement.setString(2, d.getMotdepasse());
                preparedStatement.setString(3, d.getEmail());
                preparedStatement.executeUpdate(); 
                System.out.println(sql);
            } catch (SQLException ex) {
                Logger.getLogger(UtilisateurDAO.class.getName()).log(Level.SEVERE, "insert failed", ex);
            } finally {
                try {
                    if (preparedStatement != null) {
                        preparedStatement.close();
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(UtilisateurDAO.class.getName()).log(Level.SEVERE, "free resourses failed", ex);
                }
            }
        }    

    }

    @Override
    public void update(Administrateur d) {
        if (find(d.getLogin()) != null) {

            String sql = "update administrateur set login=?,motdepasse=?,email=?";
            PreparedStatement preparedStatement = null;
            try {
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, d.getLogin());
                preparedStatement.setString(2, d.getMotdepasse());
                preparedStatement.setString(3, d.getEmail());
                preparedStatement.executeUpdate();
                System.out.println(sql);
            } catch (SQLException ex) {
                Logger.getLogger(AdministrateurDAO.class.getName()).log(Level.SEVERE, "update failed", ex);
            } finally {
                try {
                    if (preparedStatement != null) {
                        preparedStatement.close();
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(AdministrateurDAO.class.getName()).log(Level.SEVERE, "free resourses failed", ex);
                }
            }
        }
    }

    @Override
    public void delete(Administrateur d) {
        if (find(d.getLogin()) != null) {

            String sql = "DELETE FROM ADMINISTRATEUR WHERE LOGIN=?";
            PreparedStatement preparedStatement = null;
            try {
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, d.getLogin());
                preparedStatement.executeUpdate();
                                                  
                System.out.println(sql);
            } catch (SQLException ex) {
                Logger.getLogger(AdministrateurDAO.class.getName()).log(Level.SEVERE, "delete failed", ex);
            } finally {
                try {
                    if (preparedStatement != null) {
                        preparedStatement.close();
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(AdministrateurDAO.class.getName()).log(Level.SEVERE, "free resourses failed", ex);
                }
            }

        }
    }

    @Override
    public List<Administrateur> findAll() {
        List<Administrateur> list = new ArrayList<Administrateur>();
        String sql = "SELECT * FROM ADMINISTRATEUR";
        Statement statement = null; 
        ResultSet resultSet = null; 
        
        try {
            statement = connection.createStatement(); 
            resultSet = statement.executeQuery(sql);  
            System.out.println(sql);
            while (resultSet.next()) {
                list.add(new Administrateur(resultSet.getString("LOGIN"), resultSet.getString("MOTDEPASSE"), resultSet.getString("EMAIL")));
            }


        } catch (SQLException ex) {
            Logger.getLogger(AdministrateurDAO.class.getName()).log(Level.SEVERE, "find all employees failed", ex);
        } finally {            
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException ex) {
                Logger.getLogger(AdministrateurDAO.class.getName()).log(Level.SEVERE, "free resourses failed", ex);
            }
        }
        return list;
    }

   public boolean  auth(String login,String motdepasse){
       boolean test = false ;
        Administrateur found = null ;
       String sql = "SELECT LOGIN, MOTDEPASSE FROM ADMINISTRATEUR";
           
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
        try {
            
            preparedStatement = connection.prepareStatement(sql);
            
            resultSet = preparedStatement.executeQuery();
            System.out.println(sql);
             if (resultSet.next()) {
            found = new Administrateur(resultSet.getString("login"),resultSet.getString("motdepasse"));
            String l = found.getLogin();
            String  m = found.getMotdepasse();
            if  ((l.equals(login)) && (m.equals(motdepasse))){
            test = true;}
            }

 else {test = false;}

        } catch (SQLException ex) {
            Logger.getLogger(AdministrateurDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

       return test ;

       }


 public String login(){
        String l="";
            String sql = "select LOGIN from administrateur";
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;

     try {

            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            System.out.println(sql);
            if (resultSet.next()) {
                l=resultSet.getString("LOGIN");
            }
        } catch (SQLException ex) {
            Logger.getLogger(AdministrateurDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
     return l;
 }


 public Administrateur remplissage(){
 String sql = "select * from  administrateur";
 PreparedStatement ps = null;
 ResultSet rs = null;
 Administrateur admin = null ;
        try {
            ps = connection.prepareStatement(sql);
            rs = ps.executeQuery();
            if(rs.next()){             
            admin = new Administrateur(rs.getString("login"),rs.getString("motdepasse"),rs.getString("email"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(AdministrateurDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
return admin ;


 }

 public  void modifieradmin(Administrateur a){
 String sql ="update administrateur set login=?,motdepasse=?,email=?";
 PreparedStatement ps = null ;
 ResultSet rs = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1, a.getLogin());
            ps.setString(2,a.getMotdepasse());
            ps.setString(3, a.getEmail());
            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(AdministrateurDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
 }

 public void ConsulterUtilisateur(JTable table){

     PreparedStatement ps = null;
     ResultSet rs = null;
     String sql ="select * from  utilisateur";
        try {
            ps = connection.prepareStatement(sql);
           rs = ps.executeQuery();
            table.setModel(DbUtils.resultSetToTableModel(rs));
        } catch (SQLException ex) {
            Logger.getLogger(AdministrateurDAO.class.getName()).log(Level.SEVERE, null, ex);
        }


 }

 public boolean  auth1(String login){
       boolean test = false ;
        Administrateur found = null ;
       String sql = "SELECT LOGIN FROM ADMINISTRATEUR";

            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
        try {

            preparedStatement = connection.prepareStatement(sql);

            resultSet = preparedStatement.executeQuery();
            System.out.println(sql);
             if (resultSet.next()) {
            found = new Administrateur(resultSet.getString("login"));
            String l = found.getLogin();
            if  (l.equals(login)){
            test = true;}
            }

 else {test = false;}

        } catch (SQLException ex) {
            Logger.getLogger(AdministrateurDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

       return test ;

       }

 public boolean  auth2(String motdepasse){
       boolean test = false ;
        Administrateur found = null ;
       String sql = "SELECT motdepasse FROM ADMINISTRATEUR";

            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
        try {

            preparedStatement = connection.prepareStatement(sql);

            resultSet = preparedStatement.executeQuery();
            System.out.println(sql);
             if (resultSet.next()) {
            found = new Administrateur(null,resultSet.getString("motdepasse"));
            String l = found.getMotdepasse();
            if  (l.equals(motdepasse)){
            test = true;}
            }

 else {test = false;}

        } catch (SQLException ex) {
            Logger.getLogger(AdministrateurDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

       return test ;

       }

}
   
    

